/**
 * 
 */
package org.bankabeule.ui.wizards;

import org.bankabeule.BankabeuleFactory;
import org.bankabeule.Tag;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * @author sdudoit
 *
 */
public class TagPage extends WizardPage {

	
	/**
	 * Tag
	 */
	private Tag tag;
	
	
	/**
	 * Constructor
	 * @param pageName
	 */
	protected TagPage(String pageName, Tag existingTag, Tag parentTag) {
		super(pageName);
		if (existingTag == null) {
			tag = BankabeuleFactory.eINSTANCE.createTag();
			tag.setName("");
			tag.setDescription("");
		} else {
			tag = (Tag) EcoreUtil.copy(existingTag);
		}
		setTitle("Set tag properties");
		if (parentTag != null) {
			setDescription("Parent tag is: "+parentTag.getName());
		}
		setPageComplete(!tag.getName().equals(""));
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		FormToolkit tk = new FormToolkit(parent.getDisplay());
		
		parent.setLayout(new FillLayout());
		
		Composite mainComposite = tk.createComposite(parent);
		mainComposite.setLayout(new GridLayout(2, false));
		
		tk.createLabel(mainComposite, "Name").setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		Text text = tk.createText(mainComposite, tag.getName());
		text.setLayoutData(GridDataFactory.fillDefaults().create());
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				tag.setName(((Text) e.getSource()).getText());
				setPageComplete(!tag.getName().equals(""));
			}
		});
		
		Label descLabel = tk.createLabel(mainComposite, "Description");
		descLabel.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false, 2, 1));
		text = tk.createText(mainComposite, tag.getDescription(), SWT.MULTI);
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 2));
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				tag.setDescription(((Text) e.getSource()).getText());
			}
		});	
		
		setControl(parent);
	}
	
	
	/**
	 * Get tag
	 */
	public Tag getTag() {
		return tag;
	}

}
